/*
* @Author: Administrator
* @Date:   2018-09-09 19:15:27
* @Last Modified by:   Administrator
* @Last Modified time: 2018-09-11 19:39:39
*/
import React from 'react';
import { actionCreators } from './store';
import { connect } from 'react-redux';
import Topic from './components/Topic.js';
import List from './components/List.js';
import Writer from './components/Writer.js';
import Recommend from './components/Recommend.js';
import { 
	    HomeWrapper,
	    HomeLeft,
	    HomeRight,
      BackTop 
       } 
from './style.js'; 

class Home extends React.Component {
  render () {
  	return (
      <HomeWrapper>
        <HomeLeft>
          <img className="banner-img" src="//upload.jianshu.io/admin_banners/web_images/4448/4526b216a4eaac6a92b1c987ac9bb544c9f37937.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/1250/h/540" />
          <Topic />
          <List />
        </HomeLeft>
        <HomeRight>
          <Recommend />
          <Writer />
        </HomeRight>
        
        {
          this.props.scroll ? (<BackTop onClick={this.scrollTop.bind(this)}><i className="iconfont song">&#xe61d;</i></BackTop>) : null
        }
      </HomeWrapper>
  	)
  }
  scrollTop () {
    window.scrollTo(0,0);
  }
  bindEvents () {
    window.addEventListener('scroll',this.props.changeScroll)
  }
  componentWillUnMount () {
    window.removeEventListener('scroll',this.props.changeScroll)
  }
  componentDidMount () {
    this.props.ChangeHomeData(),
    this.bindEvents()
  } 
}
const mapState = (state) => ({
  scroll: state.getIn(['home','showScroll'])
})
const mapDispatch = (dispatch) => ({
   ChangeHomeData () {
     const action = actionCreators.getHomeInfo();
     dispatch(action);
   },
   changeScroll () {
    const distance = document.documentElement.scrollTop;
    if (distance > 100) {
      const action = actionCreators.changeDataScroll()
      dispatch(action);
    }
    else if (distance < 100) {
      const action = actionCreators.changeDataScrolls()
      dispatch(action);
    }
   }
})

export default connect(mapState,mapDispatch)(Home);